'    Copyright 2008 Jan Molnar <jan.molnar@abds.de>
'
'    This file is part of JCQ.

'    JCQ is free software: you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation, either version 2 of the License, or
'    (at your option) any later version.

'    JCQ is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.

'    You should have received a copy of the GNU General Public License
'    along with JCQ. If not, see <http://www.gnu.org/licenses/>.

Imports System.IO

''' <summary>
''' This ViewModel features identity editing.
''' </summary>
Public Class EditIdentityViewModel
    Private _ImageSelector As ImageSelectorViewModel
    Private _Identity As IdentityManager.IIdentity

    Public Sub New(ByVal identity As IdentityManager.IIdentity)
        _Identity = identity

        _ImageSelector = New ImageSelectorViewModel(ApplicationService.Current.DataStorageDirectory)

        If _Identity.HasAttribute(IdentityAttributes.ImageOriginalFilePathAttribute) Then
            _ImageSelector.SelectedImageFile = _Identity.GetAttribute(IdentityAttributes.ImageOriginalFilePathAttribute)
        End If
    End Sub

    ''' <summary>
    ''' Gets the ImageSelectorViewModel which allows to pick an identity image.
    ''' </summary>
    Public ReadOnly Property ImageSelector() As ImageSelectorViewModel
        Get
            Return _ImageSelector
        End Get
    End Property

    ''' <summary>
    ''' Gets the Identity which is edited.
    ''' </summary>
    Public ReadOnly Property Identity() As IdentityManager.IIdentity
        Get
            Return _Identity
        End Get
    End Property

    ''' <summary>
    ''' Updates the Identity with the specified FullName, Uin, Password and the selected image. FullName, Uin and Image
    ''' are mandatory.
    ''' </summary>
    Public Sub UpdateIdentity(ByVal fullname As String, ByVal uin As String, ByVal password As String)
        If String.IsNullOrEmpty(fullname) Then Throw New ArgumentNullException("fullname")
        If String.IsNullOrEmpty(uin) Then Throw New ArgumentNullException("uin")

        Dim avatarPath As String = System.IO.Path.Combine(ApplicationService.Current.DataStorageDirectory.FullName, String.Format("{0}.default.jpg", fullname))

        AvatarImageService.CreateAvatarImageFromFile(ImageSelector.SelectedImageFile, avatarPath)

        Identity.Identifier = fullname
        Identity.Description = uin
        Identity.ImageUrl = avatarPath
        Identity.SetAttribute(IdentityAttributes.UinAttribute, uin)

        If Not String.IsNullOrEmpty(password) Then Identity.SetAttribute(IdentityAttributes.PasswordAttribute, password)
    End Sub

    ''' <summary>
    ''' Deletes the Identity if the user agrees.
    ''' </summary>
    Public Sub DeleteIdentity()
        If MessageBox.Show(String.Format("Do you really want to delete Identity ""{0}""?", Identity.Identifier), Nothing, MessageBoxButton.YesNo, MessageBoxImage.Question) = MessageBoxResult.Yes Then
            ApplicationService.Current.IdentityProvider.DeleteIdentity(Identity)
            NavigateToSignInPage()
        End If
    End Sub

    ''' <summary>
    ''' Navigates to the SignIn page.
    ''' </summary>
    Public Sub NavigateToSignInPage()
        Core.Kernel.Services.GetService(Of INavigationService).NavigateToSignInPage()
    End Sub
End Class